package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.enumeration.VIPType;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.ICommentDataDAL;
import com.qire.manhua.model.entity.ComicComment;
import com.qire.manhua.model.viewModel.impl.ComicCommentReplyViewModel;
import com.qire.manhua.model.vo.CommentReplyVO;
import com.qire.manhua.model.vo.OkResponseVO;

import java.util.List;

/**
 * 漫画回复页数据服务
 */
public class ComicCommentReplyService extends DataService {

    private final ICommentDataDAL commentDataDAL = WarehouseFactory.create(this, ICommentDataDAL.class);

    private final ComicCommentReplyViewModel viewModel;

    public ComicCommentReplyService(ComicCommentReplyViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 加载回复评论列表数据
     * @param comicBookId 漫画ID
     * @param commentId 目标评论ID
     * @param pageNo 页号
     */
    public void loadReplyCommentListData(int comicBookId, int commentId, int pageNo) {
        commentDataDAL.fetchReplyCommentList(comicBookId, commentId, pageNo);
    }
    @StrategyAnnotation(event = Url.comment_reply_list)
    public void fetchReplyCommentListApiCallBack(List<CommentReplyVO> dataVOList, boolean isCache) {

        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataVOList)) {
            return;
        }

        if (dataVOList.isEmpty()) {
            viewModel.noMoreDataWasShown();
            return;
        }

        ComicComment[] comicComments = new ComicComment[dataVOList.size()];
        for (int index = 0; index < comicComments.length; index++) {

            CommentReplyVO replyVO = dataVOList.get(index);

            int     commentId           = replyVO.id;
            int     userId              = replyVO.userid;
            String  userName            = replyVO.user_avatar.user_name;
            String  userPortrait        = replyVO.user_avatar.user_img;
            CharSequence replyContent   = replyVO.buildReplyContent();
            String  createTime          = replyVO.create_time;
            int     replyCount          = replyVO.replie_count;
            int     likeCount           = replyVO.like_count;
            boolean isMyLike            = replyVO.like == 1;
            VIPType vipType             = VIPType.build(replyVO.user_avatar.vipType);

            comicComments[index] = new ComicComment(commentId, userId, userName, userPortrait, replyContent, createTime, replyCount, likeCount, isMyLike, vipType);
        }

        viewModel.loadComicComment(comicComments);

        if (!isCache) {
            viewModel.nextPage();
        }
    }

    /** 点赞给喜欢的评论*/
    public void voteToGiveLikeComment(int bookId, int commentId) {
        commentDataDAL.voteToGiveLikeComment(bookId, commentId);
    }
    @StrategyAnnotation(event = Url.comment_like)
    public void voteToGiveLikeCommentApiCallBack(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar(dataVO.ok, Prompt.SUCCESS);
    }

    /**
     * 举报评论
     * @see ICommentDataDAL#complaintComment(int, int, int, String)
     */
    public void complaintComment(int bookId, int commentId, int reportType, String content) {
        commentDataDAL.complaintComment(bookId, commentId, reportType, content);
    }
    @StrategyAnnotation(event = Url.comment_report)
    public void complaintCommentApiCallBack(OkResponseVO dataVO, boolean isCache) {
        if(dataVO.isOK()) {
            ToastHelper.showSnackBar("举报成功", Prompt.SUCCESS);
        } else {
            ToastHelper.showSnackBar("举报失败", Prompt.ERROR);
        }
    }

    /**
     * 回复评论
     * @param comicId 漫画ID
     * @param replyToTargetId 回复的目标评论ID
     * @param replyContent 回复的评论内容
     */
    public void replyComment(int comicId, int replyToTargetId, String replyContent) {
        commentDataDAL.submitComment(comicId, 0, replyToTargetId, replyContent, 5);
    }
    @StrategyAnnotation(event = Url.comment_add)
    public void replyCommentApiCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("评论成功", Prompt.SUCCESS);
        viewModel.refreshReplyUI();
        viewModel.loadReplyCommentListData();
    }

}
